'
'   XC Engine (project Minotaur)
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class TemplateEngine
	Private objTempateVariables
	
	Private Sub Class_Initialize()
		Set objTempateVariables = WScript.CreateObject("Scripting.Dictionary")
	End Sub
	
	Private Sub Class_Terminate()
		Set objTempateVariables = Nothing
	End Sub
	
	Public Sub PutVariable(ByVal strVariableName, ByVal strVariableValue)
		objTempateVariables.Add strVariableName, strVariableValue
	End Sub
	
	Public Sub LoadTemplate(ByVal strFileName)
		Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
		Set objFile = objFso.OpenTextFile(strFileName)
		
		strTemplate = objFile.ReadAll()
		
		objFile.Close()
		
		strTemplate = Join(Split(strTemplate, vbCrLf), "~$~")
		strRenderTemplate = "strTemplateEngineString = """""
	End Sub
	
	Private strTemplate
	Private strRenderTemplate
	
	Public Sub Render()
		intCurrentIndex = 1

		Set objRE = New RegularExpression
		
		objRE.GlobalSearch = True
		
		Do While InStr(intCurrentIndex, strTemplate, "<%") <> 0
			intStartCommandTag = InStr(intCurrentIndex, strTemplate, "<%")
			intEndCommandTag = InStr(intCurrentIndex, strTemplate, "%>")
			
			strTemplateSource = Mid(strTemplate, intCurrentIndex, intStartCommandTag-intCurrentIndex)
			strTemplateSource = Replace(strTemplateSource, """", """""")

			strRenderTemplate = strRenderTemplate & vbCrLf & "strTemplateEngineString = strTemplateEngineString & """ & strTemplateSource & """"

			strCommand = Trim(Mid(strTemplate, intStartCommandTag+2, intEndCommandTag-intStartCommandTag-2))
			
			For Each strKey In objTempateVariables.Keys
				objRE("([^a-zA-Z]{1})" & strKey & "([^a-zA-Z]{1}|$)").Replace strCommand, "$1objTempateVariables(""" & strKey & """)$2"
			Next
			
			If InStr(strCommand, "Return") > 0 Then
				strCommand = Trim(Replace(strCommand, "Return ", ""))
				strRenderTemplate = strRenderTemplate & vbCrLf & "strTemplateEngineString = strTemplateEngineString & " & strCommand
			Else
				strRenderTemplate = strRenderTemplate & vbCrLf & strCommand
			End If

			intCurrentIndex = InStr(intCurrentIndex, strTemplate, "%>")+2
		Loop
		
		strTemplateSource = Mid(strTemplate, intCurrentIndex, Len(strTemplate))
		strTemplateSource = Replace(strTemplateSource, """", """""")
		
		strRenderTemplate = strRenderTemplate & vbCrLf & "strTemplateEngineString = strTemplateEngineString & """ & strTemplateSource & """"
		
		On Error Resume Next
			Set objMsScriptControl = CreateObject("MSScriptControl.ScriptControl")

			objMsScriptControl.Language = "VBScript"
			objMsScriptControl.Timeout = -1
			
			objMsScriptControl.AddObject "objTempateVariables", objTempateVariables
			objMsScriptControl.AddCode strRenderTemplate & vbCrLf & "Function Render() : Render = strTemplateEngineString : End Function"

			strOutput = objMsScriptControl.Eval("Render()")
			
			intErrorNumber = Err.Number
		On Error Goto 0
		
		If intErrorNumber <> 0 Then Err.Raise 5000, "TemplateEngine", "Error occured while rendering the template"
		
		strOutput = Join(Split(strOutput, "~$~"), vbCrLf)
	End Sub
	
	Private strOutput
	
	Public Property Get Output
		Output = strOutput
	End Property
End Class